package android.tusnotas.activities;

import java.util.List;

import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.tusnotas.activities.extras.ShowCourseActivityExtras;
import android.tusnotas.activities.extras.ShowGroupActivityExtras;
import android.tusnotas.beans.Group;
import android.tusnotas.beans.GroupMessage;
import android.tusnotas.connector.webservice.WebServiceCaller;
import android.tusnotas.core.GroupAnalyser;
import android.tusnotas.exception.RemoteReadingException;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class ShowMessages extends CommonActivity{

	private KillReceiver mKillReceiver;

	private List<GroupMessage> messages = null;

	private String groupId;
	private String courseId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.show_messages);
		setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		//Integer result = drawActivity(getIntent());
		mKillReceiver = new KillReceiver();
		IntentFilter intentFilter = new IntentFilter("kill");
		registerReceiver(mKillReceiver,	intentFilter);
		try {
			Intent caller = getIntent();
			this.messages = caller.getParcelableArrayListExtra(ShowGroupActivityExtras.GROUP_MESSAGES_KEY);
			this.groupId = caller.getExtras().getString(ShowGroupActivityExtras.GROUP_ID);
			this.courseId = caller.getExtras().getString(ShowCourseActivityExtras.COURSE_ID);
			drawMessages();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	public void drawMessages(){
		LinearLayout lyt = (LinearLayout) findViewById(R.id.group_messages_table);
		lyt.removeAllViews();

		TextView textViewName = null;
		TextView textViewDate = null;
		TextView textViewContent = null;

		DisplayMetrics displaymetrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
		int width = displaymetrics.widthPixels;

		//ViewGroup.LayoutParams layoutParams = ((TextView) findViewById(R.id.group_messages)).getLayoutParams();

		for(GroupMessage message : this.messages){

			// fecha
			textViewDate = new TextView(getApplicationContext());
			textViewDate.setGravity(Gravity.RIGHT);
			textViewDate.setText(message.getDate()+" ");
			textViewDate.setTextColor(Color.WHITE);
			lyt.addView(textViewDate);
			// nombre
			textViewName = new TextView(getApplicationContext());
			textViewName.setGravity(Gravity.LEFT);
			textViewName.setText(message.getUser()+":");
			textViewName.setTextColor(Color.WHITE);
			lyt.addView(textViewName);
			// mensaje
			textViewContent = new TextView(getApplicationContext());
			textViewContent.setSingleLine(false);
			textViewContent.setWidth(width);
			textViewContent.setGravity(Gravity.CENTER_VERTICAL);
			textViewContent.setText(message.getText()+"\n");
			textViewContent.setTextColor(Color.BLACK);
			lyt.addView(textViewContent);
			// separator line
			TextView separator = new TextView(getApplicationContext());
			separator.setBackgroundColor(Color.GRAY);
			separator.setHeight(2);
			separator.setWidth(width);
			lyt.addView(separator);
		}
	}

	public void sendMessage(View view){

		Thread t = new Thread( new Runnable() {
			@Override
			public void run() {
				WebServiceCaller caller = new WebServiceCaller();
				EditText messageView = (EditText)findViewById(R.id.messageText);
				String messageText = messageView.getText().toString();
				if (!messageText.equals("")){
					
					caller.setSendMessage(groupId, messageText);
					try {
						caller.execute();
					} catch (RemoteReadingException e) {
						e.printStackTrace();
						Toast t = Toast.makeText(getApplicationContext(),R.string.toast_message_error, Toast.LENGTH_SHORT);
						t.setGravity(Gravity.CENTER, 0, 0);
						t.show();
					}
					// everything ok
					messageView.setText("");
					refreshMessages();
					Toast t = Toast.makeText(getApplicationContext(),R.string.toast_message_ok, Toast.LENGTH_SHORT);
					t.setGravity(Gravity.CENTER, 0, 0);
					t.show();
				} else { // empty message
					Toast t = Toast.makeText(getApplicationContext(),R.string.toast_message_empty, Toast.LENGTH_SHORT);
					t.setGravity(Gravity.CENTER, 0, 0);
					t.show();
				}
			}
		});
		t.run();

	} 
	
	private void refreshMessages(){
		
		// read from group webservice
		GroupAnalyser groupAnalyser = new GroupAnalyser();
		try {
			Group auxGroup = new Group();
			auxGroup = groupAnalyser.doGetGroup(this.courseId);
			this.messages = auxGroup.getMessages();
			//remove everything from the view
//			LinearLayout table = (LinearLayout) findViewById(R.id.group_messages_table);
//			table.removeAllViews();

			drawMessages();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	

	//private void initilizeThread(){	
	//	pd = ProgressDialog.show(this, 
	//			getResources().getString(R.string.process_dialog_title_loading),
	//			getResources().getString(R.string.process_dialog_message_loading),
	//			Boolean.TRUE, Boolean.FALSE);
	//	this.thread = new Thread( new Runnable() {
	//		@Override
	//		public void run() {
	//			pd.dismiss();
	//			h.sendEmptyMessage(0);
	//		}
	//	});


}
